package com.xqy.wexmini.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xqy.wexmini.domain.Vegetable;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ElasticService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    //创建索引
    public void CreateIndex(String index) throws IOException {
        CreateIndexRequest request=new CreateIndexRequest(index);
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
    }
    //判断索引是否存在
    public boolean ExistIndex(String index) throws IOException {
        GetIndexRequest request=new GetIndexRequest(index);
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        return exists;
    }
    //批量插入
    public <T> void testBulkRequest(List<T> userList,String index) throws IOException {
        BulkRequest bulkRequest=new BulkRequest();
        bulkRequest.timeout("10s");
        for (int i=0;i<userList.size();i++) {
            //批量更新和批量删除就在这里修改对应的请求就可以了
            bulkRequest.add(new IndexRequest(index)
                    .id(""+(i+1))
                    .source(JSON.toJSONString(userList.get(i)), XContentType.JSON)
            );
        }
        BulkResponse bulkResponse=restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
    }
    //更新
    public <T> void testUpdateRequest(String index,String id,T t) throws IOException {
        UpdateRequest updateRequest=new UpdateRequest(index,id);
        updateRequest.timeout("1s");
        updateRequest.doc(JSON.toJSONString(t),XContentType.JSON);
        UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
    }
    //删除
    public void testDeleteRequest(String index,String id) throws IOException {
        DeleteRequest deleteRequest=new DeleteRequest(index,id);
        deleteRequest.timeout("1s");
        DeleteResponse deleteResponse=restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);
    }
    //查找
    public void testSearch(String index,String name) throws IOException {
        SearchRequest searchRequest=new SearchRequest(index);
        //构建搜索条件
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        //查询条件，可以使用QueryBuilders工具实现
        //QueryBuilders.termQuery精确
        //QueryBuilders.matchAllQuery()匹配
        //使用其他需要修改这个
        //earchSourceBuilder.query(QueryBuilders.multiMatchQuery(describes,"name","describes"));
        searchSourceBuilder.query(QueryBuilders.matchQuery("name",name));
//        searchSourceBuilder.from();
//        searchSourceBuilder.size();
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse=restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        System.out.println("==================================");
        for(SearchHit documentFields:searchResponse.getHits().getHits()){
//            Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
            String s="[";
            s=s+documentFields.getSourceAsString()+"]";
            List<Vegetable> vegetables = (List<Vegetable>) JSONArray.parseArray(s, Vegetable.class);
            System.out.println(vegetables.get(0));
            break;
        }
    }
}
